﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.IO;
using System.Configuration;

namespace LibraryUtility.Common
{
    /// <summary>
    /// ClassName:Comm
    /// Auther:Leo.wl
    /// Time:2010-04-15
    /// </summary>
    [System.Diagnostics.DebuggerStepThrough()]
    public class Common
    {
        /// <summary>
        /// Fetches the page.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <returns></returns>
        static public string FetchPage(string url)
        {
            String result = String.Empty;
            try
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Encoding enc = Encoding.GetEncoding("utf-8");
                using (StreamReader sr = new StreamReader(response.GetResponseStream(), enc))
                {
                    result = sr.ReadToEnd();
                    sr.Dispose();
                }
            }
            catch
            {
                throw;
            }
            return result;
        }
        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="o">The o.</param>
        /// <returns></returns>
        static public string GetValue(object o)
        {
            List<DataConfigItem> items = ConfigurationManager.GetSection("DataConfig") as List<DataConfigItem>;
            DataConfigItem config = items.Find(delegate(DataConfigItem d) { return d.Key == o.ToString(); });
            return config.Value;
        }
        /// <summary>
        /// Replaces the HTML.
        /// </summary>
        /// <param name="s">The s.</param>
        /// <returns></returns>
        static public string ReplaceHTML(string s)
        {
            return
            s.Replace("<", "&lt;")
             .Replace(">", "&gt;")
             .Replace("'", "‘")
             .Replace("\"", "”");
        }


        /// <summary>
        /// Gets the cache timer.
        /// </summary>
        /// <value>The cache timer.</value>
        static public int CacheTimer
        {
            get
            {
                return int.Parse(GetValue("CacheTimer"));
            }
        }
    }
}
